package com.zr.corporate.controller;

import com.zr.corporate.pojo.*;
import com.zr.corporate.service.CorporateFactoryService;
import com.zr.planclass.pojo.PlanClassAddVo;
import com.zr.planclass.pojo.PlanClassSelectVo;
import com.zr.util.ResultVo;
import com.zr.util.ResultVoBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/9/24.
 */
@RestController
@CrossOrigin //支持跨域
public class CorporateFactoryController {

    @Autowired
    private CorporateFactoryService corporateFactoryService;

    /**
     *  法人名称下拉框
     */
    @GetMapping("legalPerson/ofCurrentUser")
    public ResultVo getLegaalPerson(){
        Integer userId=1;
        ResultVo resultVo =  corporateFactoryService.queryCorporateByUserId(userId);
        return resultVo;
    }


    @PostMapping("legalPlant/query")//@RequestBody接受json格式，@Valid做表单验证， BindResult接受错误提示信息
    public ResultVo queryPage(@RequestBody CorporateFactorySelectVo corporateFactorySelectVo) {
        ResultVo resultVo = corporateFactoryService.queryPage(corporateFactorySelectVo);
        return resultVo;
    }

    @PostMapping("legalPlant/add")//@RequestBody接受json格式，@Valid做表单验证， BindResult接受错误提示信息
    public ResultVo add(@RequestBody @Valid CorporateFactoryAddVo corporateFactoryAddVo, BindingResult bindingResult) {
        //判断是否存在错误信息
        if (bindingResult.hasErrors()) {
            //如果存在return给页面错误信息
            return ResultVoBuilder.error("500", bindingResult.getFieldError().getDefaultMessage());
        }
        List<CorporateFactoryAddVo> corporateFactoryAddVoList = new ArrayList<>();
        corporateFactoryAddVoList.add(corporateFactoryAddVo);
        //如果没有错误跳到service层进行逻辑处理
        ResultVo resultVo = corporateFactoryService.add(corporateFactoryAddVoList);
        return resultVo;
    }


    @PostMapping("legalPlant/updatestatus")//@RequestBody接受json格式，@Valid做表单验证， BindResult接受错误提示信息
    public ResultVo updateStatus(@RequestBody CorporateFactoryUpdateStatusVo corporateFactoryUpdateStatusVo, BindingResult bindingResult) {
        //判断是否存在错误信息
        if (bindingResult.hasErrors()) {
            //如果存在return给页面错误信息
            return ResultVoBuilder.error("500", bindingResult.getFieldError().getDefaultMessage());
        }
        //如果没有错误跳到service层进行逻辑处理
        ResultVo resultVo = corporateFactoryService.updateStatus(corporateFactoryUpdateStatusVo);
        return resultVo;
    }

    @PostMapping("legalPlant/update")//@RequestBody接受json格式，@Valid做表单验证， BindResult接受错误提示信息
    public ResultVo update(@RequestBody @Valid CorporateFactoryUpdateVo corporateFactoryUpdateVo, BindingResult bindingResult) {
        //判断是否存在错误信息
        if (bindingResult.hasErrors()) {
            //如果存在return给页面错误信息
            return ResultVoBuilder.error("500", bindingResult.getFieldError().getDefaultMessage());
        }
        //如果没有错误跳到service层进行逻辑处理
        ResultVo resultVo = corporateFactoryService.update(corporateFactoryUpdateVo);
        return resultVo;
    }

    @PostMapping("quotation/FactoryInquiryIntroduction")
    public ResultVo importFile(MultipartFile file)throws Exception{
        return corporateFactoryService.importFile(file);
    }

    @GetMapping("/exportExcel/master")
    public ResultVo exportExcel(HttpServletResponse resp, String LegalPersonCode, String LegalPlantCode,String LegalPlantDec, Integer Enabled) throws Exception{
        CorporateFactorySelectVo corporateFactorySelectVo = new CorporateFactorySelectVo();
        corporateFactorySelectVo.setLegalPersonCode(LegalPersonCode);
        corporateFactorySelectVo.setLegalPlantCode(LegalPlantCode);
        corporateFactorySelectVo.setLegalPlantDec(LegalPlantDec);
        corporateFactorySelectVo.setEnabled(Enabled);
        return corporateFactoryService.exportExcel(resp,corporateFactorySelectVo);
    }

}
